package com.base.controller;

import com.base.po.voip.Rewards;
import com.base.po.voip.Withdrawals;
import com.base.service.RewardsService;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import com.base.service.WithdrawalsService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 顺通进出账记录
 *
 */
@Controller
@RequestMapping("/admin")
public class index_STRewards {
    @Autowired
    private RewardsService rewardsService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private SubscribersService subscribersService;
    @Autowired
    private WithdrawalsService withdrawalsService;


    // 自营商城订单首页
    @RequestMapping("/stAddRewards")
    public ModelAndView stAddRewards(HttpServletRequest request, String userid, Integer type) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<Rewards> rewards = null;

        Page page = new Page();
        page.setCurrentPageNo(1);
        // 最多显示1000条
        page.setRecordTotal(rewardsService.countBetweenType(userid, 2, 4));
        rewards = rewardsService.selectBetweenType(page, userid, 2, 4);

        request.setAttribute("list", rewards);
        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("userid", userid);
        request.setAttribute("type", type);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("adminUsergrade", adminUsergrade);

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_STAddRewards");
        return modelAndView;
    }

    @RequestMapping("/selectSTAddRewards")
    @ResponseBody
    public ModelAndView selectSTAddRewards(HttpServletRequest request, String userid, Integer type, Integer curPage) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<Rewards> rewards = null;

        Page page = new Page();
        page.setCurrentPageNo(curPage);
        // 最多显示1000条
        page.setRecordTotal(rewardsService.countBetweenType(userid, 2, 4));
        rewards = rewardsService.selectBetweenType(page, userid, 2, 4);

        request.setAttribute("list", rewards);
        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("userid", userid);
        request.setAttribute("type", type);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("adminUsergrade", adminUsergrade);

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_STAddRewards");
        return modelAndView;
    }

    // 自营商城订单首页
    @RequestMapping("/stCutRewards")
    public ModelAndView stCutRewards(HttpServletRequest request, String userid, Integer type) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<Withdrawals> withdrawals = null;

        Page page = new Page();
        page.setCurrentPageNo(1);
        // 最多显示1000条
        page.setRecordTotal(withdrawalsService.selectByCount(userid));
        withdrawals = withdrawalsService.selectByAll(page, userid);

        BigDecimal rate = new BigDecimal(0.92);
        for (Withdrawals w : withdrawals){
            w.setMoney(w.getMoney().divide(rate,10,BigDecimal.ROUND_HALF_UP).setScale(2,BigDecimal.ROUND_DOWN));
        }

        request.setAttribute("list", withdrawals);
        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("userid", userid);
        request.setAttribute("type", type);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("adminUsergrade", adminUsergrade);

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_STCutRewards");
        return modelAndView;
    }

    @RequestMapping("/selectSTCutRewards")
    public ModelAndView selectSTCutRewards(HttpServletRequest request, String userid, Integer type, Integer curPage) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<Withdrawals> withdrawals = null;

        Page page = new Page();
        page.setCurrentPageNo(curPage);
        // 最多显示1000条
        page.setRecordTotal(withdrawalsService.selectByCount(userid));
        withdrawals = withdrawalsService.selectByAll(page, userid);

        BigDecimal rate = new BigDecimal(0.92);
        for (Withdrawals w : withdrawals){
            w.setMoney(w.getMoney().divide(rate, 10,BigDecimal.ROUND_HALF_UP).setScale(2,BigDecimal.ROUND_DOWN));
        }

        request.setAttribute("list", withdrawals);
        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("userid", userid);
        request.setAttribute("type", type);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("adminUsergrade", adminUsergrade);

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_STAddRewards");
        return modelAndView;
    }
}
